<template>
	<view class="navbar">
		<view class="navbar-fixed">
			<!-- 状态栏占位 -->
			<view :style="{height: statusBarHeight+'px'}"></view>
			<!-- 导航栏内容 -->
			<view class="navbar-content" :class="{search:isSearch}" :style="{height:navbarHeight+'px',width:windowWidth+'px'}" @click.stop="open">
				<ivew v-if="isSearch" @click="back" class="navbar-content-back">
					<uni-icons type="back" size="22" color="#fff"></uni-icons>
				</ivew>
				<view class="navbar-search" v-if="!isSearch">
					<!--  非搜索页显示 -->
					<view class="navbar-search-icon">
						<uni-icons type="search" size="16" color="#999"></uni-icons>
					</view>
					<view class="navbar-search-text">uni-app</view>
				</view>
				<view v-else class="navbar-search">
					<!-- 搜索页面显示 -->
					<input type="text" value="" placeholder="请输入搜索内容" v-model="val" @input="inputChange"/>
				</view>
			</view>
		</view>
		<!-- 占位状态栏+导航栏的高度,使下面的内容不会被遮挡 -->
		<view :style="{height:(statusBarHeight + navbarHeight)+'px'}"></view>
	</view>
</template>

<script>
	export default {
		name: "navbar",
		props:{
			value:{
				type:[String,Number],
				default:''
			},
			isSearch:{
				type:Boolean,
				default:false
			}
		},
		data() {
			return {
				statusBarHeight: 20,
				navbarHeight: 45,
				windowWidth: 375,
				val:''
			};
		},
		watch:{
			value(newVal){
				this.val = newVal
			}
		},
		// 组件加载
		created() {
			// 同步获取手机系统信息
			let info = uni.getSystemInfoSync()
			// 动态设置状态栏高度
			this.statusBarHeight = info.statusBarHeight
			// 设置搜索框的宽度
			this.windowWidth = info.windowWidth

			/* 条件编译 不等于这三个平台 , 只会在微信小程序,QQ,百度小程序运行*/
			// #ifndef H5 || MP-ALIPAY || APP-PLUS
			/* 动态设置导航栏的高度 */
			// 获取胶囊位置
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			// 导航栏高度 = (胶囊底部 - 状态栏高度) + (胶囊顶部 - 状态栏高度)
			this.navbarHeight = (menuButtonInfo.bottom - this.statusBarHeight) + (menuButtonInfo.top - this
				.statusBarHeight)
			// 设置搜索框的宽度
			this.windowWidth = menuButtonInfo.left
			// #endif
		},
		methods:{
			open(){
				if(this.isSearch) return
				uni.navigateTo({
					url: '/pages/home-search/home-search'
				});
			},
			// 监听输入框的变化
			inputChange(e){
				let {value} = e.detail
				this.$emit('input',value)
			},
			back(){
				uni.switchTab({
					url:'/pages/tabbar/index/index'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.navbar {
		.navbar-fixed {
			// 固定定位
			position: fixed;
			top: 0;
			left: 0;
			z-index: 999;
			width: 100%;
			background-color: $mk-base-color;

			.navbar-content {
				display: flex;
				// 水平居中
				justify-content: center;
				// 垂直居中
				align-items: center;
				padding: 0 15px;
				// 盒内显示 (父元素和子元素宽度都是100%,但是父元素加了左右内填充,会导致子元素不在父元素内)
				box-sizing: border-box;

				.navbar-search {
					display: flex;
					// flex-direction默认水平排列
					flex-direction: row;
					// 垂直居中
					align-items: center;
					height: 30px;
					width: 100%;
					border-radius: 30px;
					padding: 0 10px;
					background-color: #FFFFFF;

					.navbar-search-icon {
						margin-right: 10px;
					}

					.navbar-search-text {
						font-size: 12px;
						color: #999;
					}
				}
				// 同级元素,重复覆盖上面的样式
				&.search{
					padding-left: 0;
					.navbar-content-back{
						margin-left: 10px;
						margin-right: 10px;
					}
					.navbar-search{
						border-radius: 5px;
					}
					input {
						width: 100%;
					}
				}
			}
		}
	}
</style>
